package com.yao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yao.common.Result;
import com.yao.po.Flowers;
import com.yao.po.ShoppingCart;
import com.yao.service.IFlowersService;
import com.yao.service.IShoppingCartService;
import com.yao.utils.UserContext;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/cart")
public class CartShopping {

    private static final Logger log = LoggerFactory.getLogger(CartShopping.class);
    private final IShoppingCartService shoppingCartService;
    private final IFlowersService flowersService;
    public CartShopping(IShoppingCartService shoppingCartService, IFlowersService flowersService) {
        this.shoppingCartService = shoppingCartService;
        this.flowersService = flowersService;
    }

    /**
     * 添加购物车
     * @return  Result
     */
    @GetMapping("/addToCart/{goodsId}/{count}")
    public Result<Object> addCart(String blessing, @PathVariable("goodsId") Integer goodsId, @PathVariable("count") Integer count ) {
        log.info("添加购物车商品...");
        ShoppingCart shoppingCart = shoppingCartService.lambdaQuery().eq(ShoppingCart::getOpenId, UserContext.getUser())
                .eq(ShoppingCart::getGoodsId, goodsId).one();

        if(shoppingCart!= null){
            //判断是否存在该商品，如果存在，则更新数量
            LambdaUpdateWrapper<ShoppingCart> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(ShoppingCart::getOpenId, UserContext.getUser())
            .eq(ShoppingCart::getGoodsId, goodsId)
            .set(ShoppingCart::getCount, count+shoppingCart.getCount())
            .set(ShoppingCart::getBlessing, blessing);
            shoppingCartService.update(updateWrapper);
        }else {
            //不存在该商品，则新增
            Flowers flower = flowersService.getById(goodsId);
            if(flower == null){
                return Result.error("商品不存在");
            }
            shoppingCart = new ShoppingCart();
            shoppingCart.setOpenId(UserContext.getUser());
            shoppingCart.setGoodsId(goodsId);
            shoppingCart.setCount(count);
            shoppingCart.setBlessing(blessing);
            shoppingCart.setPrice(flower.getPrice());
            shoppingCart.setImageUrl(flower.getImageUrl());
            shoppingCart.setName(flower.getName());
            shoppingCartService.save(shoppingCart);
        }
        return Result.success("添加购物车成功");
    }

    /**
     * 获取购物车列表
     * @return  Result
     */
    @GetMapping("/getCartList")
    public Result<Object> getCart() {
        log.info("获取购物车列表...");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<ShoppingCart> eq = queryWrapper.eq(ShoppingCart::getOpenId, UserContext.getUser());
        return Result.success(shoppingCartService.list(eq));
    }

    /**
     * 设置购物车商品是否选中
     * @param goodsId 商品ID
     * @param isChecked 是否选中
     * @return  Result
     */
    @GetMapping("/checkCart/{goodsId}/{isChecked}")
    public Result<Object> setIsCheck(@PathVariable("goodsId") Integer goodsId, @PathVariable("isChecked") Integer isChecked) {
        log.info("设置购物车商品是否选中...");
        LambdaUpdateWrapper<ShoppingCart> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ShoppingCart::getOpenId, UserContext.getUser())
                .eq(ShoppingCart::getGoodsId, goodsId)
                .set(ShoppingCart::getIsChecked, isChecked);
        boolean update = shoppingCartService.update(updateWrapper);
        if (!update) {
            return Result.error("设置购物车商品是否选中失败");
        }
        return Result.success();
    }

    /**
     * 设置购物车全部商品反选
     * @param isChecked 反选
     * @return  Result
     */
    @GetMapping("/checkAllCart/{isChecked}")
    public Result<Object> checkAllCart(@PathVariable("isChecked") Integer isChecked) {
        log.info("设置购物车全部商品反选...");
        LambdaUpdateWrapper<ShoppingCart> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ShoppingCart::getOpenId, UserContext.getUser())
                .set(ShoppingCart::getIsChecked, isChecked);
        boolean update = shoppingCartService.update(updateWrapper);
        if (!update) {
            return Result.error("设置购物车全部商品反选失败");
        }
        return Result.success();
    }

    /**
     * 删除购物车商品
     * @param goodsId 商品ID
     * @return  Result
     */
    @GetMapping("/delete/{goodsId}")
    public Result<Object> deleteCart(@PathVariable("goodsId") Integer goodsId) {
        log.info("删除购物车商品...");
         shoppingCartService.remove(new LambdaQueryWrapper<ShoppingCart>()
                .eq(ShoppingCart::getOpenId, UserContext.getUser())
                .eq(ShoppingCart::getGoodsId, goodsId));
         return Result.success();
    }

}
